<template>
  <div class="header-container">
    <div class="header-left">
      <div class="logo-container">
        <img src="/阿里云.png" alt="Logo" class="logo-img" />
      </div>
      <div class="company-name">阿里巴巴集团OA中台系统</div>
      <div class="menu-icon">
        <img src="/1.png" alt="Menu" class="menu-img" />
      </div>
    </div>
    <div class="header-right">
      <!-- <div class="search-box">搜索应用或帮助</div> -->
      <div class="header-icons">
        <div class="icon-item">
          <img src="/4.png" alt="Icon" class="header-icon" />
        </div>
        <div class="icon-item">
          <img src="/5.png" alt="Icon" class="header-icon" />
        </div>
        <div class="icon-item">
          <img src="/2.png" alt="Icon" class="header-icon" />
        </div>
      </div>
      <div class="user-info" @click="handleUserClick">
        {{ username }}
      </div>
    </div>
  </div>

  <!-- 退出登录对话框 -->
  <el-dialog v-model="dialogVisible" title="退出登录" width="300">
    <span>确认要退出吗？</span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 个人信息对话框 -->
  <el-dialog v-model="userInfoDialogVisible" title="个人信息" width="400">
    <div class="user-info-container" v-if="userData">
      <div class="user-info-item">
        <span class="info-label">工号：</span>
        <span class="info-value">{{ userData.id }}</span>
      </div>
      <div class="user-info-item">
        <span class="info-label">姓名：</span>
        <span class="info-value">{{ userData.username }}</span>
      </div>
      <div class="user-info-item">
        <span class="info-label">部门：</span>
        <span class="info-value">{{ userData.department }}</span>
      </div>
      <div class="user-info-item">
        <span class="info-label">职位：</span>
        <span class="info-value">{{ userData.position }}</span>
      </div>
      <div class="user-info-item">
        <span class="info-label">手机：</span>
        <span class="info-value">{{ userData.phone }}</span>
      </div>
      <div class="user-info-item">
        <span class="info-label">员工类型：</span>
        <span class="info-value">{{ userData.employeestype }}</span>
      </div>
      <div class="user-info-item">
        <span class="info-label">员工状态：</span>
        <span class="info-value">{{ userData.employees }}</span>
      </div>
      <div class="user-info-item">
        <span class="info-label">出生日期：</span>
        <span class="info-value">{{ userData.birthdate }}</span>
      </div>
      <div class="user-info-item">
        <span class="info-label">入职日期：</span>
        <span class="info-value">{{ userData.date }}</span>
      </div>
      <div class="user-info-item">
        <span class="info-label">转正日期：</span>
        <span class="info-value">{{ userData.confirmationdate }}</span>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="userInfoDialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="showLogoutDialog">退出登录</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import router from "@/router";
import { ref, onBeforeMount } from "vue";
import { getUser } from "../utils/api";

let username = ref<string>("去登录");
let userData = ref(null);
let user = localStorage.getItem("data");

onBeforeMount(() => {
  if (user) {
    const parsedUser = JSON.parse(user);
    username.value = parsedUser.username;
    userData.value = parsedUser;
  }
});

const dialogVisible = ref(false);
const userInfoDialogVisible = ref(false);

function handleUserClick() {
  if (username.value === "去登录") {
    router.push("/login");
  } else {
    // 显示个人信息弹框
    userInfoDialogVisible.value = true;
  }
}

function showLogoutDialog() {
  userInfoDialogVisible.value = false;
  dialogVisible.value = true;
}

function goLogin() {
  if (username.value === "去登录") {
    router.push("/login");
  } else {
    dialogVisible.value = true;
  }
}

function confirm() {
  dialogVisible.value = false;
  localStorage.removeItem("data");
  localStorage.removeItem("token");
  router.push("/login");
}
</script>

<style scoped>
.header-container {
  width: 100%;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30px;
  background-color: #263961;
  box-sizing: border-box;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo-container {
  background-color: #fff;
  border-radius: 5px;
  padding: 4px;
}

.logo-img {
  width: 20px;
  height: 20px;
}

.company-name {
  font-size: 20px;
  font-weight: 800;
  color: #fff;
  margin: 0 10px;
}

.menu-img {
  width: 24px;
  height: 24px;
  margin-top: 5px;
}

.header-right {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 12vw;
}

.search-box {
  background-color: #fff;
  border-radius: 10px;
  width: 380px;
  height: 40px;
  line-height: 40px;
  color: gray;
  padding: 0 20px;
}

.header-icons {
  display: flex;
  align-items: center;
}

.icon-item {
  margin: 0 10px;
}

.header-icon {
  width: 24px;
  height: 24px;
  margin-top: 5px;
}

.user-info {
  font-size: 16px;
  font-weight: 800;
  color: #fff;
  margin: 0 10px;
  position: relative;
  cursor: pointer;
}

/* 用户信息弹框样式 */
.user-info-container {
  padding: 10px;
}

.user-info-item {
  margin-bottom: 12px;
  display: flex;
}

.info-label {
  font-weight: bold;
  width: 80px;
  display: inline-block;
  text-align: right;
  margin-right: 10px;
}

.info-value {
  flex: 1;
}

/* 自定义Element Plus按钮颜色 */
:deep(.el-button--primary) {
  background-color: #203A64 !important;
  border-color: #203A64 !important;
}

:deep(.el-button--primary:hover),
:deep(.el-button--primary:focus) {
  background-color: #2c4e85 !important;
  border-color: #2c4e85 !important;
}

/* 移动端适配 */
@media (max-width: 992px) {
  .header-container {
    padding: 0 15px;
  }

  .header-right {
    width: auto;
  }

  .search-box {
    width: 200px;
  }

  .company-name {
    font-size: 16px;
  }
}

@media (max-width: 768px) {
  .header-container {
    height: auto;
    flex-direction: column;
    padding: 10px 15px;
  }

  .header-left,
  .header-right {
    width: 100%;
    justify-content: space-between;
  }

  .header-right {
    margin-top: 10px;
  }

  .search-box {
    width: 100%;
    max-width: 300px;
  }

  .header-icons {
    margin-left: 10px;
  }

  .icon-item {
    margin: 0 5px;
  }
}

@media (max-width: 576px) {
  .company-name {
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 150px;
  }

  .search-box {
    max-width: 150px;
    font-size: 12px;
  }

  .logo-img,
  .menu-img,
  .header-icon {
    width: 20px;
    height: 20px;
  }

  .user-info {
    width: 100px;
    /* background-color: red !important; */
    font-size: 14px;
    margin-right: 10px;
  }
}

@media (max-width: 480px) {
  .header-container {
    padding: 8px 10px;
  }

  .header-icons {
    display: none; /* 在极小屏幕上隐藏图标 */
  }

  .company-name {
    max-width: 120px;
  }
}
</style>
